home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / arith / iio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.7 KB  |  245 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  iio.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. /*    iio.c,        RD, 20.02.90 */
  16.  
  17. #include <LEDA/impl/iint.h>
  18. #include <LEDA/impl/iloc.h>
  19. #include <stdio.h>
  20. #include <ctype.h>
  21.  
  22. int fscanI(fp, a)
  23.     FILE *fp;
  24.     pInteger a;
  25. {    register int c, count;
  26.     int sign=PLUS;
  27.     Integer help;
  28.  
  29.     cI(&help);
  30.     count=0;
  31.     do {
  32.         c=getc(fp);
  33.         count++;
  34.     } while (isspace(c));
  35.     if (c=='-') {
  36.         sign=MINUS;
  37.         c=getc(fp);
  38.         count++;
  39.     } else if (c=='+') {
  40.         c=getc(fp);
  41.         count++;
  42.     }
  43.     while (c=='0') {
  44.         c=getc(fp);
  45.         count++;
  46.     }
  47.     Iasint(a, 0);
  48.     while (isdigit(c)) {
  49.         ImuasP(a, 10);
  50.         Iasint(&help, c-'0');
  51.         IplasI(a, &help);
  52.         c=getc(fp);
  53.         count++;
  54.     }
  55.     ungetc(c, fp);
  56.     count--;
  57.     if (!Ieq0(a))
  58.         a->sign=sign;
  59.     dI(&help);
  60.     return count;
  61. }        /* fscanI */
  62.  
  63. int fprintI(fp, a)
  64.     FILE *fp;
  65.     const Integer *a;
  66. {    char *s;
  67.     int i, sl, count=0;
  68.     Integer help;
  69.  
  70.     sl=Ilog(a);
  71.     sl=sl/3+2;
  72.     s=(char*)Imalloc(sl*sizeof(char));
  73.     if (!s)
  74.         Ierror("fprintI: no memory available\n");
  75. #ifdef USE_DIRECT_OUTPUT_CONVERSION
  76.     if (sl==-1) {
  77.         putc('0', fp);
  78.         count++;
  79.         return count;
  80.     }
  81.     if (Ilt0(a)) {
  82.         putc('-', fp);
  83.         count++;
  84.     }
  85.     cIasI(&help, a);
  86.     i=0;
  87.     while (!Ieq0(&help)) {
  88.         s[i]='0'+uIdiasP(&help, 10);
  89.         i++;
  90.     }
  91.     dI(&help);
  92.     i--;
  93.     for (; i>=0; i--) {
  94.         putc(s[i], fp);
  95.         count++;
  96.     }
  97. #else
  98.     count=Itoa(a, s);
  99.     for ( i=0; i<count; i++ )
  100.         putc(s[i], fp);
  101. #endif
  102.     Ifree(s);
  103.     return count;
  104. }        /* fprintI */
  105.  
  106. /******************************************/
  107.  
  108. char * wIdata1(a, l)
  109.     const Integer * a;
  110.     int * l;
  111. {    *l=sizeof(int) + sizeof(int);
  112.     return (char *) (& a->length);
  113. }
  114.  
  115. char * wIdata2(a, l)
  116.     const Integer * a;
  117.     int * l;
  118. {    *l=a->length * sizeof(PLACE);
  119.     return (char *) a->vec;
  120. }
  121.  
  122. char * rIdata1(a, l)
  123.     const Integer * a;
  124.     int * l;
  125. {    *l=sizeof(int) + sizeof(int);
  126.     return (char *) (& a->length);
  127. }
  128.  
  129. char * rIdata2(a, l)
  130.     Integer * a;
  131.     int * l;
  132. {    int nl=a->length;
  133.     if (nl > a->maxlength) {
  134.         delvec(a->vec, a->maxlength);
  135.         a->maxlength=nl;
  136.         a->vec=newvec(&a->maxlength);
  137.     }
  138.     *l=nl * sizeof(PLACE);
  139.     return (char *) a->vec;
  140. }
  141.  
  142. /************************************/
  143.  
  144. #if HALFRADIX > 1000000000
  145. #define DECCONV 1000000000
  146. #define NCONV    9
  147. #else
  148. #define DECCONV 10000
  149. #define NCONV    4
  150. #endif
  151.  
  152. int Itoa(n, s)
  153.     const Integer *n;
  154.     char s[];
  155. {    int count=0;
  156.     Integer help;
  157.     char *p, *q, c;
  158.  
  159.     if (Ieq0(n)) {
  160.         s[count]='0';
  161.         count++;
  162.         s[count]='\0';
  163.         return count;
  164.     }
  165.     if (Ilt0(n)) {
  166.         s[count]='-';
  167.         count++;
  168.     }
  169.     cIasI(&help, n);
  170.     while (!Ieq0(&help)) {
  171.         int rem=uIdiasP(&help, DECCONV);
  172.         if (Ieq0(&help)) {    /* skip leading 0 */
  173.             while ( rem ) {
  174.                 s[count]='0'+ rem % 10;
  175.                 count++;
  176.                 rem /= 10;
  177.             }
  178.         } else {        /* print leading 0 */
  179.             int i;
  180.             for (i=0; i<NCONV; i++) {
  181.                 s[count]='0'+ rem % 10; 
  182.                 count++; 
  183.                 rem /= 10; 
  184.             }   
  185.         }
  186.     }
  187.     dI(&help);
  188.     /* Vertausche Stringeintraege, damit hoeherwertige Stellen
  189.         vorne stehen.
  190.     */
  191.     if (Ilt0(n))
  192.         p=&s[1];
  193.     else
  194.         p=&s[0];
  195.     q=&s[count-1];
  196.     while (p<q) {
  197.         c=*p;
  198.         *p++=*q;
  199.         *q--=c;
  200.     }
  201.     s[count]='\0';
  202.     return count;
  203. }        /* Itoa */
  204.  
  205. int atoI(s, n)
  206.     char s[];
  207.     Integer *n;
  208. {    register int c, count;
  209.     Integer help;
  210.     int sign=PLUS;
  211.  
  212.     cI(&help);
  213.     count=0;
  214.     do {
  215.         c=s[count];
  216.         count++;
  217.     } while (isspace(c));
  218.     if (c=='-') {
  219.         sign=MINUS;
  220.         c=s[count];
  221.         count++;
  222.     } else if (c=='+') {
  223.         c=s[count];
  224.         count++;
  225.     }
  226.     while (c=='0') {
  227.         c=s[count];
  228.         count++;
  229.     }
  230.     Iasint(n, 0);
  231.     while (isdigit(c)) {
  232.         ImuasP(n, 10);
  233.         Iasint(&help, c-'0');
  234.         IplasI(n, &help);
  235.         c=s[count];
  236.         count++;
  237.     }
  238.     count--;
  239.     if (!Ieq0(n))
  240.         n->sign=sign;
  241.     dI(&help);
  242.     return count;
  243. }        /* atoI */
  244.